package mobi.greenplate.theweb.user;

import mobi.greenplate.theweb.domain.User;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author dmakariev
 */
public class UserUtils {

    public static Long getUserId(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
            return ((User)principal).getId();
        }
        return null;
    }

    public static String getUserName(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
            return ((User)principal).getUserName();
        }
        if(principal instanceof String){
            return (String)principal;
        }
        return null;
    }

    //not real user, if needed , Configurable should be used with userRepository...
    //this is enough for saving user depending object
    public static User currentUser(){
        return new User(getUserId(),getUserName());
    }

    public static String getPassword(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
            return ((User)principal).getPassword();
        }
        return null;
    }

}
